package com.chenyue.cm.common.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenyue.cm.account.domain.JdAccountRecord;
import com.chenyue.cm.base.aop.Log;
import com.chenyue.cm.base.bean.PageVo;
import com.chenyue.cm.base.bean.Pageable;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.utils.RequestUtil;
import com.chenyue.cm.common.service.JdHotKeyWordsService;
import com.chenyue.cm.common.domain.JdHotKeyWords;
import com.chenyue.cm.common.domain.JdHotKeyWordsExample;
import com.chenyue.cm.msg.domain.JdMsg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
* @author chenyue-cm
* @date 2021-11-11
*/
@RestController
@RequestMapping("/jdHotKeyWords")
public class JdHotKeyWordsController {

    private final JdHotKeyWordsService jdHotKeyWordsService;

    public JdHotKeyWordsController(JdHotKeyWordsService jdHotKeyWordsService) {
        this.jdHotKeyWordsService = jdHotKeyWordsService;
    }

    @RequestMapping("/getList")
    public Success getList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
		PageVo<JdHotKeyWords>  result  = jdHotKeyWordsService.getPageBean(pageInfo, JdHotKeyWords.class);
		return Success.success(result);
    }

    @RequestMapping("/getJdHotKeyWordsList")
    public Success getJdHotKeyWordsList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        //设置分页信息
        PageHelper.startPage(pageInfo.getPageIndex(), pageInfo.getPageSize(), true);
        LambdaQueryWrapper<JdHotKeyWords> wrapper = new LambdaQueryWrapper<>();
        Map<String, Object> pageSearch = pageInfo.getPageSearch();
        if (pageSearch != null) {
            if (null != pageSearch.get("SEARCH_LIKE_name")) {
                wrapper.like(JdHotKeyWords::getName, String.valueOf(pageSearch.get("SEARCH_LIKE_name")));
            }
        }
        wrapper.orderByAsc(JdHotKeyWords::getOrderNo);
        List<JdHotKeyWords> list = jdHotKeyWordsService.list(wrapper);
        //封装返回结果
        PageInfo<JdHotKeyWords> result = new PageInfo<>(list);

        return Success.success(result);
    }

    @RequestMapping("/getNewOrder")
    public Success getNewOrder() {
        LambdaQueryWrapper<JdHotKeyWords> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(JdHotKeyWords::getOrderNo);
        List<JdHotKeyWords> list = jdHotKeyWordsService.list(wrapper);
        int orderNo = 1;
        if(list != null && list.size()>0){
            orderNo = list.get(0).getOrderNo()+1;
        }
        return Success.success(orderNo);
    }

    /**
     * @param vo 插入实体
     *
     * @return Success
     */
    @Log
    @RequestMapping("/save")
    public Success save(JdHotKeyWords vo) {
        boolean success = jdHotKeyWordsService.save(vo);
        return success ? Success.success() : Success.error();
    }

    /**
    * @param vo 修改实体
    * @param request id
    *
    * @return Success
    */
   @Log
   @RequestMapping("/edit")
   public Success edit(JdHotKeyWords vo, HttpServletRequest request) {
       String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            int success = jdHotKeyWordsService.getBaseMapper().updateById(vo);
            return success > 0 ? Success.success() : Success.error();
        } else {
          return Success.error("id不能为空");
        }
    }

    /**
     *
     * @param id 主键
     *
     * @return Success
     */
    @Log
    @RequestMapping("/delete/{id}")
    public Success deleteByPrimaryKey(@PathVariable("id") Integer id) {
        JdHotKeyWordsExample aExample = new JdHotKeyWordsExample();
        aExample.createCriteria().andIdEqualTo(id);
        int success = jdHotKeyWordsService.deleteByExample(aExample);
        return success > 0 ? Success.success() : Success.error();
    }

 	/**
	 * 【API】----  删除多个数据
     *
	 * @return Success
	 */
    @Log
    @RequestMapping("/delete")
	public Success delete(String ids) {
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            int success = jdHotKeyWordsService.deleteByPrimaryKey(idString);
            if (success == 0) {
                return Success.error();
            }
        }
        return Success.success();
	}

}
